package com.gomtel.smartdevice.base;

import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.gomtel.smartdevice.bean.PulseConditionBeanPlus;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/3.
 */
@SuppressWarnings("ALL")
public abstract class BasicAdapter<T> extends BaseAdapter {

    protected String TAG = getClass().getSimpleName();

    protected ArrayList<T> list;

    public BasicAdapter(ArrayList<T> list) {
        super();
        this.list = list == null ? new ArrayList<>() : list;
    }

    @Override
    public int getCount() {
        int size = list.size();
        if (list.size() > 0 && (list.get(0) instanceof PulseConditionBeanPlus)) {
            size = 3;
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        if (list.size() > 0 && (list.get(0) instanceof PulseConditionBeanPlus)) {
            position = 0;
        }
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BasicHolder<T> holder = null;
        if (convertView == null) {
            holder = getHolder(position);
        } else {
            holder = (BasicHolder) convertView.getTag();
        }
        if (list.size() > 0 && (list.get(0) instanceof PulseConditionBeanPlus)) {
            holder.bindData(new Pair<>(position, list));
        } else {
            holder.bindData(list.get(position));
        }
        return holder.getHolderView();
    }

    protected abstract BasicHolder<T> getHolder(int position);

}
